package com.twotiger.and.activity.current;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;
import app.twotiger.p2p.R.color;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.TwoTigerApp;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.adapter.OperationRecordAdapter;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.DataList;
import com.twotiger.and.bean.OperationRecord;
import com.twotiger.and.util.TimeUtils;
import com.twotiger.and.util.ViewUtils;
import com.view.pulltorefresh.MyListView;
import com.view.pulltorefresh.PullToRefreshBase;
import com.view.pulltorefresh.PullToRefreshBase.OnLastItemVisibleListener;
import com.view.pulltorefresh.PullToRefreshMyListView;

/**
 * 活期-操作记录
 * @Description: 
 * intent需要携带 PROJECT_Id or PROJECT_DETAIL
 * @Author douxy
 * @Date 2015年8月12日 下午2:43:22
 */
public class CurrentOperationRecordPage extends BaseActivity implements OnClickListener {

    private static final int GETOPERATIONSUCCSE = 0;
    private static final int GETOPERATIONFAIL = 1;

    private static final String TYPE_IN = "in";
    private static final String TYPE_OUT = "out";

    private static HashMap<String, String> params;
    private int pNum = 1;
    private SubTitle subTitle;
    private RadioGroup operation_record_radiogroup;
    private RadioButton operation_record_rdio_1, operation_record_rdio_2, operation_record_rdio_3;
    private View operation_record_tab_1, operation_record_tab_2, operation_record_tab_3;
    private RelativeLayout no_content_rl;
    private PullToRefreshMyListView operation_record_refresh_list;
    private ImageView iv_no_content;

    private final int WITCH_ALL = 0;
    private final int WITCH_IN = 1;
    private final int WITCH_OUT = 2;
    private int witch_tag;
    private OperationRecordAdapter mOperationRecordAdapter;

    private String type;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.current_operation_record_list_layout, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.setTitle("操作记录");
        subTitle.hideBtns();
        subTitle.title_left_bt.setVisibility(View.VISIBLE);

        operation_record_radiogroup = (RadioGroup) view
            .findViewById(R.id.operation_record_radiogroup);
        operation_record_rdio_1 = (RadioButton) view.findViewById(R.id.operation_record_rdio_1);
        operation_record_rdio_2 = (RadioButton) view.findViewById(R.id.operation_record_rdio_2);
        operation_record_rdio_3 = (RadioButton) view.findViewById(R.id.operation_record_rdio_3);
        operation_record_tab_1 = (View) view.findViewById(R.id.operation_record_tab_1);
        operation_record_tab_2 = (View) view.findViewById(R.id.operation_record_tab_2);
        operation_record_tab_3 = (View) view.findViewById(R.id.operation_record_tab_3);
        iv_no_content = (ImageView) view.findViewById(R.id.iv_no_content);

        no_content_rl = (RelativeLayout) view.findViewById(R.id.no_content_rl);
        operation_record_refresh_list = (PullToRefreshMyListView) view
            .findViewById(R.id.operation_record_refresh_list);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.no_content_rl:
            refreshlist();
            break;
        case R.id.operation_record_rdio_1:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            if (witch_tag == WITCH_ALL) {
                return;
            }
            witch_tag = WITCH_ALL;
            refreshHead(witch_tag);
            break;
        case R.id.operation_record_rdio_2:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            if (witch_tag == WITCH_IN) {
                return;
            }
            witch_tag = WITCH_IN;
            refreshHead(witch_tag);
            break;
        case R.id.operation_record_rdio_3:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            if (witch_tag == WITCH_OUT) {
                return;
            }
            witch_tag = WITCH_OUT;
            refreshHead(witch_tag);
            break;
        default:
            break;

        }
    }

    @Override
    protected void setListener() {

        handler = new BaseHandler(this) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case GETOPERATIONSUCCSE:
                    Basebean result = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result.isOk()) {
                        operation_record_refresh_list.getLoadingLayoutProxy().setLastUpdatedLabel(
                            "最后更新：今天 " + TimeUtils.getFormatDate(" HH:mm"));
                        DataList dataList = JSON.parseObject(result.data, DataList.class);
                        ArrayList<OperationRecord> operationRecordList = (ArrayList<OperationRecord>) JSON
                            .parseArray(dataList.getList(), OperationRecord.class);
                        if (!operationRecordList.isEmpty()) {
                            if(operationRecordList.size() < Integer.parseInt(Constants.pageSize)){
                                operation_record_refresh_list.changeMyFooterViewContent(false);
                            }else{
                                operation_record_refresh_list.changeMyFooterViewContent(true);
                            }
                            no_content_rl.setVisibility(View.GONE);
                            if (pNum == 1) {
                                mOperationRecordAdapter.resetData(operationRecordList);
                            } else {
                                mOperationRecordAdapter.addData(operationRecordList);
                            }
                        } else {
                            //请求数据是空的
                            if (pNum == 1) {
                                no_content_rl.setVisibility(View.VISIBLE);
                                iv_no_content.setImageResource(R.drawable.no_content);
                            } else {
                                operation_record_refresh_list.changeMyFooterViewContent(false);
                                mOperationRecordAdapter.notifyDataSetChanged();
                            }
                        }
                    } else {
                        showToast(result.codeDesc);
                    }

                    operation_record_refresh_list.onRefreshComplete();
                    break;
                case GETOPERATIONFAIL:

                    operation_record_refresh_list.onRefreshComplete();
                    break;
                default:
                    break;
                }

                super.handleMessage(msg);
            }
        };
        operation_record_refresh_list.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

            @Override
            public void onLastItemVisible() {
                String footerContent = ((TextView)operation_record_refresh_list.getChildviewOfFooterview()).getText().toString();
                if("已无更多项目".equals(footerContent)){
                    return;
                }
                /**
                 * 加载更多数据
                 */
                 pNum++;
                 refreshlist();
            }
        });

        operation_record_refresh_list
            .setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<MyListView>() {
                @Override
                public void onPullDownToRefresh(PullToRefreshBase<MyListView> refreshView) {
                    /**
                     * 刷新数据
                     */
                    pNum = 1;
                    refreshlist();
                }

                @Override
                public void onPullUpToRefresh(PullToRefreshBase<MyListView> refreshView) {
                    /**
                    * 加载更多数据
                    */
                    pNum++;
                    refreshlist();
                }
            });

        no_content_rl.setOnClickListener(this);
        operation_record_rdio_1.setOnClickListener(this);
        operation_record_rdio_2.setOnClickListener(this);
        operation_record_rdio_3.setOnClickListener(this);
    }

    @Override
    protected void processLogic() {
        params = getParm();
        refreshHead(witch_tag);
        ArrayList<OperationRecord> dataList = new ArrayList<OperationRecord>();
        mOperationRecordAdapter = new OperationRecordAdapter(CurrentOperationRecordPage.this,
            dataList);
        operation_record_refresh_list.setAdapter(mOperationRecordAdapter);

    }

    private void refreshlist() {
        //请求之前先看是否开通活期,如果没有则不用请求，直接显示无记录布局
        if (!TwoTigerApp.getInstance().getUser().hasOpenProjectXAct) {
            no_content_rl.setVisibility(View.VISIBLE);
            iv_no_content.setImageResource(R.drawable.no_content);
            return;
        }
        
        params.clear();
        params.put("token", getToken());
        params.put("pageNum", String.valueOf(pNum));
        params.put("pageSize", Constants.pageSize);
        params.put("timestamp", TimeUtils.getTimeStamp());
        params.put("type", type);
        getDate(params, Constants.OPERATEXQUERY, handler, GETOPERATIONSUCCSE,
            GETOPERATIONFAIL, true, true, false);
    }

    /**
     * 刷新头显示,并且做相关请求
     */
    private void refreshHead(int witch) {

        switch (witch) {
        case WITCH_ALL:
            operation_record_rdio_1.setTextColor(getResources().getColor(color.orange));
            operation_record_tab_1.setVisibility(View.VISIBLE);
            operation_record_rdio_2.setTextColor(getResources().getColor(color.black));
            operation_record_tab_2.setVisibility(View.GONE);
            operation_record_rdio_3.setTextColor(getResources().getColor(color.black));
            operation_record_tab_3.setVisibility(View.GONE);

            type = null;
            break;
        case WITCH_IN:
            operation_record_rdio_1.setTextColor(getResources().getColor(color.black));
            operation_record_tab_1.setVisibility(View.GONE);
            operation_record_rdio_2.setTextColor(getResources().getColor(color.orange));
            operation_record_tab_2.setVisibility(View.VISIBLE);
            operation_record_rdio_3.setTextColor(getResources().getColor(color.black));
            operation_record_tab_3.setVisibility(View.GONE);

            type = TYPE_IN;
            break;
        case WITCH_OUT:
            operation_record_rdio_1.setTextColor(getResources().getColor(color.black));
            operation_record_tab_1.setVisibility(View.GONE);
            operation_record_rdio_2.setTextColor(getResources().getColor(color.black));
            operation_record_tab_2.setVisibility(View.GONE);
            operation_record_rdio_3.setTextColor(getResources().getColor(color.orange));
            operation_record_tab_3.setVisibility(View.VISIBLE);

            type = TYPE_OUT;
            break;
        default:
            break;
        }
        pNum = 1;
        refreshlist();
    }

    @Override
    protected void onBackPress() {
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

}
